# coding: utf-8
# @Author : lryself
# @Date : 2022/1/25 18:07
# @Software: PyCharm
import yaml


def readYML(filepath: str):
    """
    打开yaml文件
    :param filepath: 文件路径
    :return:
    """
    with open(filepath, 'r', encoding="utf-8") as f:
        file_data = f.read()

    # 将字符串转化为字典或列表
    data = yaml.load(file_data, Loader=yaml.FullLoader)
    return data


def get_database_url():
    driver_dict = {
        'mysql': 'pymysql',
        'mssql': 'pymssql',
        'oracle': 'cx_oracle',
        'postgresql': 'psycopg2'
    }
    database_config = readYML("../configs/database.yml")
    return '{}+{}://{}:{}@{}:{}/{}?charset=utf8'.format(
        database_config["dialect"],
        driver_dict[database_config["dialect"]],
        database_config["username"],
        database_config["password"],
        database_config["host"],
        database_config["port"],
        database_config["database"]
    )
